package com.example.yilanchatserver.application.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.example.yilanchatserver.application.controller.vo.UtopiaKnowledgeContentListVO;
import com.example.yilanchatserver.business.freechat.service.UtopiaService;
import com.example.yilanchatserver.business.freechat.service.ZhiPuService;
import com.example.yilanchatserver.business.freechat.service.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.springframework.http.ResponseEntity;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

@RestController
@RequestMapping("/metah/yilan")
@Slf4j
public class YiLanKnowledgeBaseController {

    @Resource
    private UtopiaService utopiaService;

    @Resource
    private ZhiPuService zhiPuService;


    @PostMapping("/knowledgeBase/list")
    public YiLanGetKnowledgeBaseListVO getKnowledgeList(@RequestBody YiLanGetKnowledgeBaseListDTO yiLanGetKnowledgeBaseListDTO) throws Exception {
        if(Objects.equals(yiLanGetKnowledgeBaseListDTO.getService(), "lingxin")){
            List<YiLanKnowledgeData> yiLanKnowledgeDataList =  utopiaService.getKnowledgeList(yiLanGetKnowledgeBaseListDTO);
            YiLanGetKnowledgeBaseListVO yiLanGetKnowledgeBaseListVO = new YiLanGetKnowledgeBaseListVO();
            yiLanGetKnowledgeBaseListVO.setList(yiLanKnowledgeDataList);
            yiLanGetKnowledgeBaseListVO.setTotal(yiLanKnowledgeDataList.size());
            return yiLanGetKnowledgeBaseListVO;
        } else if (Objects.equals(yiLanGetKnowledgeBaseListDTO.getService(), "zhipu")) {
            return zhiPuService.getKnowledgeBaseList(yiLanGetKnowledgeBaseListDTO);
        } else {
            throw new Exception("不存在的服务！");
        }
    }

//    @GetMapping("/knowledgeBase/get")
//    public YiLanKnowledgeData getKnowledgeBase(@RequestParam String id) throws Exception {
//        return zhiPuService.getKnowledgeBase(id);
//    }

    @PostMapping("/knowledgeBase/create")
    public String createKnowledge(@RequestBody YiLanCreateKnowledgeBaseDTO yiLanCreateKnowledgeBaseDTO) throws Exception {
        if(Objects.equals(yiLanCreateKnowledgeBaseDTO.getService(), "lingxin")){
            return utopiaService.createKnowledge(yiLanCreateKnowledgeBaseDTO).toString();
        } else if (Objects.equals(yiLanCreateKnowledgeBaseDTO.getService(), "zhipu")) {
            return zhiPuService.createKnowledgeBase(yiLanCreateKnowledgeBaseDTO);
        } else {
            throw new Exception("不存在的服务！");
        }
    }

    @PostMapping("/knowledgeBase/update")
    public void updateKnowledge(@RequestBody YiLanCreateKnowledgeBaseDTO yiLanCreateKnowledgeBaseDTO) throws Exception {
        if(Objects.equals(yiLanCreateKnowledgeBaseDTO.getService(), "lingxin")){
            utopiaService.updateKnowledge(yiLanCreateKnowledgeBaseDTO);
        } else if (Objects.equals(yiLanCreateKnowledgeBaseDTO.getService(), "zhipu")) {
            zhiPuService.updateKnowledgeBase(yiLanCreateKnowledgeBaseDTO);
        } else {
            throw new Exception("不存在的服务！");
        }

    }

    @PostMapping("/knowledgeBase/delete")
    public void deleteKnowledge(@RequestBody YiLanDeleteKnowledgeDTO yiLanDeleteKnowledgeDTO) throws Exception {
        if(Objects.equals(yiLanDeleteKnowledgeDTO.getService(), "lingxin")){
            utopiaService.deleteKnowledge(yiLanDeleteKnowledgeDTO);
        } else if (Objects.equals(yiLanDeleteKnowledgeDTO.getService(), "zhipu")) {
            zhiPuService.deleteKnowledgeBase(yiLanDeleteKnowledgeDTO.getId().toString());
        } else {
            throw new Exception("不存在的服务！");
        }
    }

    @PostMapping("/knowledge/list")
    public YiLanGetKnowledgeDocumentListVO getKnowledgeContentList(@RequestBody YiLanKnowledgeContentListDTO yiLanKnowledgeContentListDTO) throws Exception{
        if(Objects.equals(yiLanKnowledgeContentListDTO.getService(), "lingxin")) {
            UtopiaKnowledgeContentListVO utopiaKnowledgeContentListVO = utopiaService.getKnowledgeContentList(yiLanKnowledgeContentListDTO);
            List map = utopiaKnowledgeContentListVO.getContent();
            JSONArray list = JSONArray.parseArray(JSON.toJSONString(map));
            List<YiLanDocumentData> yiLanDocumentDataList = list.toJavaList(YiLanDocumentData.class);
            YiLanGetKnowledgeDocumentListVO yiLanGetKnowledgeDocumentListVO = new YiLanGetKnowledgeDocumentListVO();
            yiLanGetKnowledgeDocumentListVO.setList(yiLanDocumentDataList);
            yiLanGetKnowledgeDocumentListVO.setTotal(yiLanDocumentDataList.size());
            return yiLanGetKnowledgeDocumentListVO;
        } else if (Objects.equals(yiLanKnowledgeContentListDTO.getService(), "zhipu")) {
            ZhiPuGetKnowledgeDocumentListDTO zhiPuGetKnowledgeDocumentListDTO = new ZhiPuGetKnowledgeDocumentListDTO();
            zhiPuGetKnowledgeDocumentListDTO.setKnowledge_id(yiLanKnowledgeContentListDTO.getFaqId().toString());
            zhiPuGetKnowledgeDocumentListDTO.setSize(yiLanKnowledgeContentListDTO.getPageSize().toString());
            zhiPuGetKnowledgeDocumentListDTO.setPage(yiLanKnowledgeContentListDTO.getPageNum());
            return zhiPuService.getknowledgeDocumentList(zhiPuGetKnowledgeDocumentListDTO);
        } else {
            throw new Exception("不存在的服务！");
        }
    }

    @PostMapping("/knowledge/create")
    public YiLanUploadUrlVO createKnowledgeContent(@RequestBody YiLanCreateKnowledgeContentDTO yiLanCreateKnowledgeContentDTO) throws Exception {
        YiLanUploadUrlVO yiLanUploadUrlVO = new YiLanUploadUrlVO();
        if(Objects.equals(yiLanCreateKnowledgeContentDTO.getService(), "lingxin")){
            Map map = utopiaService.createKnowledgeContent(yiLanCreateKnowledgeContentDTO);
            ZhiPuDocumentSuccessData zhiPuDocumentSuccessData = new ZhiPuDocumentSuccessData();
            zhiPuDocumentSuccessData.setDocumentId((String) map.get("id"));
            zhiPuDocumentSuccessData.setUrl((String) map.get("url"));
            zhiPuDocumentSuccessData.setFileName((String) map.get("content"));
            List<ZhiPuDocumentSuccessData> zhiPuDocumentSuccessDataList = new ArrayList<>();
            zhiPuDocumentSuccessDataList.add(zhiPuDocumentSuccessData);
            yiLanUploadUrlVO.setSuccessInfos(zhiPuDocumentSuccessDataList);
            return yiLanUploadUrlVO;
        } else if (Objects.equals(yiLanCreateKnowledgeContentDTO.getService(), "zhipu")) {
            ZhiPuUploadUrlDTO zhiPuUploadUrlDTO = JSON.parseObject(JSON.toJSONString(yiLanCreateKnowledgeContentDTO), ZhiPuUploadUrlDTO.class);
            zhiPuUploadUrlDTO.setKnowledge_id(yiLanCreateKnowledgeContentDTO.getFaqId().toString());
            return zhiPuService.knowledgeUploadDocument(zhiPuUploadUrlDTO);
        } else {
            throw new Exception("不存在的服务！");
        }
    }

    @PostMapping("/knowledge/update")
    public void updateKnowledgeContent(@RequestBody YiLanUpDateKnowledgeContentDTO yiLanCreateKnowledgeContentDTO) throws Exception {
        if(Objects.equals(yiLanCreateKnowledgeContentDTO.getService(), "lingxin")){
            utopiaService.createKnowledgeContent(JSON.parseObject(JSON.toJSONString(yiLanCreateKnowledgeContentDTO), YiLanCreateKnowledgeContentDTO.class));
        } else if (Objects.equals(yiLanCreateKnowledgeContentDTO.getService(), "zhipu")) {
            zhiPuService.knowledgeEditDocument(JSON.parseObject(JSON.toJSONString(yiLanCreateKnowledgeContentDTO), ZhiPuUploadDocumentDTO.class));
        } else {
            throw new Exception("不存在的服务！");
        }
    }

    @PostMapping("/knowledge/get")
    public YiLanDocumentData getKnowledgeContent(@RequestBody YiLanGetKnowledgeContentDTO yiLanGetKnowledgeContentDTO) throws Exception {
        if(Objects.equals(yiLanGetKnowledgeContentDTO.getService(), "lingxin")){
            Map map = utopiaService.getKnowledgeContent(yiLanGetKnowledgeContentDTO);
            return JSON.parseObject(JSON.toJSONString(map), YiLanDocumentData.class);
        } else if (Objects.equals(yiLanGetKnowledgeContentDTO.getService(), "zhipu")) {
            return zhiPuService.getknowledgeDocument(yiLanGetKnowledgeContentDTO.getId());
        } else {
            throw new Exception("不存在的服务！");
        }

    }

    @PostMapping("/knowledge/delete")
    public void deleteKnowledgeContent(@RequestBody YiLanDeleteKnowledgeContentDTO yiLanDeleteKnowledgeContentDTO) throws Exception {
        if(Objects.equals(yiLanDeleteKnowledgeContentDTO.getService(), "lingxin")) {
            utopiaService.deleteKnowledgeContent(yiLanDeleteKnowledgeContentDTO);
        } else if (Objects.equals(yiLanDeleteKnowledgeContentDTO.getService(), "zhipu")) {
            zhiPuService.deleteKnowledgeDocument(yiLanDeleteKnowledgeContentDTO.getId());
        }
    }

    @PostMapping("/space/create")
    public YiLanCreateSpaceVO addSpace(@RequestBody YiLanCreateSpaceDTO yiLanCreateSpaceDTO) throws Exception {
        if(Objects.equals(yiLanCreateSpaceDTO.getService(), "lingxin")) {
            Map map = utopiaService.createSpace(yiLanCreateSpaceDTO);
            YiLanCreateSpaceVO yiLanCreateSpaceVO = new YiLanCreateSpaceVO();
            yiLanCreateSpaceVO.setSpaceId(((Integer) map.get("id")).toString());
            yiLanCreateSpaceVO.setName((String) map.get("roleNames"));
            return yiLanCreateSpaceVO;

        } else if (Objects.equals(yiLanCreateSpaceDTO.getService(), "zhipu")) {
            return zhiPuService.createSpace(yiLanCreateSpaceDTO);
        } else {
            throw new Exception("不存在的服务！");
        }

    }

    @PostMapping("/application/update")
    public void updateRole(@RequestBody YiLanUpdateRoleDTO yiLanUpdateRoleDTO) throws Exception {
        log.info("更新角色");
        if(Objects.equals(yiLanUpdateRoleDTO.getService(), "lingxin")) {
            utopiaService.updateRole(yiLanUpdateRoleDTO);
        } else if (Objects.equals(yiLanUpdateRoleDTO.getService(), "zhipu")) {
            ZhiPuApplicationData zhiPuApplicationData = new ZhiPuApplicationData();
            zhiPuApplicationData.setId(yiLanUpdateRoleDTO.getId().toString());
            zhiPuApplicationData.setKnowledge_ids(yiLanUpdateRoleDTO.getFaqs());
            System.out.println(zhiPuApplicationData);
            zhiPuService.editApplication(zhiPuApplicationData);
        } else {
            throw new Exception("不存在的服务！");
        }
    }
}
